package org.example.lethear.repository;

import org.example.lethear.entity.Comment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 评论数据访问接口
 */
@Repository
public interface CommentRepository extends JpaRepository<Comment, Long> {
    
    /**
     * 根据帖子ID查找评论
     */
    List<Comment> findByPostId(Long postId);
    
    /**
     * 根据帖子ID分页查找评论
     */
    Page<Comment> findByPostId(Long postId, Pageable pageable);
    
    /**
     * 根据帖子ID和状态查找评论
     */
    Page<Comment> findByPostIdAndStatus(Long postId, Integer status, Pageable pageable);
    
    /**
     * 根据用户ID查找评论
     */
    List<Comment> findByUserId(Long userId);
    
    /**
     * 根据用户ID分页查找评论
     */
    Page<Comment> findByUserId(Long userId, Pageable pageable);
    
    /**
     * 根据用户ID和状态查找评论
     */
    Page<Comment> findByUserIdAndStatus(Long userId, Integer status, Pageable pageable);
    
    /**
     * 根据父评论ID查找回复评论
     */
    List<Comment> findByParentId(Long parentId);
    
    /**
     * 根据父评论ID和状态查找回复评论
     */
    List<Comment> findByParentIdAndStatus(Long parentId, Integer status);
    
    /**
     * 查找顶级评论（父评论ID为0或null）
     */
    @Query("SELECT c FROM Comment c WHERE (c.parentId = 0 OR c.parentId IS NULL) AND c.postId = :postId AND c.status = 1 ORDER BY c.createTime ASC")
    Page<Comment> findTopLevelComments(@Param("postId") Long postId, Pageable pageable);
    
    /**
     * 查找帖子的所有评论（包括回复）
     */
    @Query("SELECT c FROM Comment c WHERE c.postId = :postId AND c.status = 1 ORDER BY c.createTime ASC")
    List<Comment> findAllCommentsByPostId(@Param("postId") Long postId);
    
    /**
     * 根据状态查找评论
     */
    Page<Comment> findByStatus(Integer status, Pageable pageable);
    
    /**
     * 搜索评论内容
     */
    @Query("SELECT c FROM Comment c WHERE c.status = 1 AND c.content LIKE %:keyword%")
    Page<Comment> searchComments(@Param("keyword") String keyword, Pageable pageable);
    
    /**
     * 查找指定时间段内的评论
     */
    @Query("SELECT c FROM Comment c WHERE c.status = 1 AND c.createTime BETWEEN :startTime AND :endTime")
    Page<Comment> findCommentsByCreateTimeBetween(@Param("startTime") LocalDateTime startTime, 
                                                 @Param("endTime") LocalDateTime endTime, 
                                                 Pageable pageable);
    
    /**
     * 查找最新评论
     */
    @Query("SELECT c FROM Comment c WHERE c.status = 1 ORDER BY c.createTime DESC")
    Page<Comment> findRecentComments(Pageable pageable);
    
    /**
     * 查找热门评论（根据点赞数）
     */
    @Query("SELECT c FROM Comment c WHERE c.status = 1 ORDER BY c.likeCount DESC")
    Page<Comment> findHotComments(Pageable pageable);
    
    /**
     * 增加评论点赞数
     */
    @Modifying
    @Query("UPDATE Comment c SET c.likeCount = c.likeCount + 1 WHERE c.id = :commentId")
    int incrementLikeCount(@Param("commentId") Long commentId);
    
    /**
     * 减少评论点赞数
     */
    @Modifying
    @Query("UPDATE Comment c SET c.likeCount = c.likeCount - 1 WHERE c.id = :commentId AND c.likeCount > 0")
    int decrementLikeCount(@Param("commentId") Long commentId);
    
    /**
     * 统计帖子的评论数
     */
    @Query("SELECT COUNT(c) FROM Comment c WHERE c.postId = :postId AND c.status = 1")
    long countByPostId(@Param("postId") Long postId);
    
    /**
     * 统计用户的评论数
     */
    @Query("SELECT COUNT(c) FROM Comment c WHERE c.userId = :userId AND c.status = 1")
    long countByUserId(@Param("userId") Long userId);
    
    /**
     * 统计某个评论的回复数
     */
    @Query("SELECT COUNT(c) FROM Comment c WHERE c.parentId = :parentId AND c.status = 1")
    long countReplies(@Param("parentId") Long parentId);
    
    /**
     * 统计今日评论数
     */
    @Query("SELECT COUNT(c) FROM Comment c WHERE c.createTime >= :startOfDay AND c.status = 1")
    long countTodayComments(@Param("startOfDay") LocalDateTime startOfDay);
    
    /**
     * 查找用户在指定帖子下的评论
     */
    @Query("SELECT c FROM Comment c WHERE c.postId = :postId AND c.userId = :userId AND c.status = 1")
    List<Comment> findByPostIdAndUserId(@Param("postId") Long postId, @Param("userId") Long userId);
    
    /**
     * 删除帖子的所有评论（软删除）
     */
    @Modifying
    @Query("UPDATE Comment c SET c.status = 0 WHERE c.postId = :postId")
    int deleteByPostId(@Param("postId") Long postId);
    
    /**
     * 获取评论树结构（递归查询所有回复）
     */
    @Query("SELECT c FROM Comment c WHERE c.postId = :postId AND c.status = 1 ORDER BY c.parentId ASC, c.createTime ASC")
    List<Comment> findCommentTree(@Param("postId") Long postId);
    
    /**
     * 根据状态统计评论数量
     */
    long countByStatus(Integer status);
} 